package cn.kshsot.zqyg.common.log.aspect;

import cn.kshost.zqyg.model.entity.system.SysOperLog;
import cn.kshsot.zqyg.common.log.Log;
import cn.kshsot.zqyg.common.log.service.AsyncOperLogService;
import cn.kshsot.zqyg.common.utils.LogUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.Ordered;

@Aspect
public class LogAspect implements Ordered {
    @Autowired
    AsyncOperLogService asyncOperLogService;

    @Around("@annotation(syLog)")
    public Object doConcurrentOperation(ProceedingJoinPoint pjp, Log syLog) throws Throwable {
        //获取操作标题
        SysOperLog sysOperLog = new SysOperLog();
        LogUtil.beforeHandleLog(syLog,pjp,sysOperLog);
        Object proceed =null;
        try {
            proceed = pjp.proceed();
            //正常返回通知
            LogUtil.afterHandlLog(syLog,proceed,sysOperLog,0,null);
        }catch (Throwable e){
            e.printStackTrace();
            //异常通知
            LogUtil.afterHandlLog(syLog,proceed,sysOperLog,1,e.getMessage());
        }finally {
            //插入日志到数据库
            asyncOperLogService.saveSysOperLog(sysOperLog);
            return proceed;
        }
    }

    @Override
    public int getOrder() {

        return -10;
    }
}
